import storage from "@/global/storage";
import utils from '@/global/utils';
import request from "@/global/request/axios";
import { login as Api } from "@/global/request/api";

const TOKEN = storage.get('token');

const page = [
    {
        path: "/login",
        component: () => import('@/views/Login.vue'),
        name: "Login"
    },
    {
        path: "/",
        name: "Root",
        component: () => import('@/components/layout/BasicLayout.vue'),
        // redirect: { name: "Home" },
        children: []
    },
    {
        path: "/404",
        name: "Error404",
        component: () => import('@/views/Error404.vue')
    },
    {
        path: "*",
        redirect: { name: "Error404" }
    }
];

export default page.map(async routes => {
    if (TOKEN && routes.children) {

        const { code, data } = await request.get(Api.getAuthMenuList);

        if (code === 200) {
            routes.children = utils.addRouterAuth(data);
            storage.set('menuList', JSON.stringify(data));
        }

    }

    return routes;
});
